home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / CASIO.C < prev    next >
Text File  |  1993-06-03  |  8KB  |  351 lines

  1. /* Casio watch game        Steve Ward 1/82
  2.  *
  3.  * The object of the game is to "hit" digits that roll across a display
  4.  * by incrementing a master digit (using the '4' key) and 'shooting' (with
  5.  * the '6' key) every time the master digit matches one or more of the digits
  6.  * in the display. After a certain number of digits have been 'hit', another
  7.  * round will begin in which the digits roll by faster. If the row of digits
  8.  * manages to completely fill up the display, which happens when you aren't
  9.  * shooting fast enough, the round will end and you probably lose points. 
  10.  * Points are scored for every hit, but points are also decucted for 'missing'
  11.  * (when you shoot with a digit that doesn't exist in the display; this makes
  12.  * it impossible to cheat by just incrementing and shooting as fast as you can
  13.  * which would otherwise be a pretty good strategy.)
  14.  */
  15.  
  16. #include "bdscio.h"
  17. #define    SIZE    6        /* Size of shift array            */
  18. #define    TICS    80        /* Time scale -- adjust to processor speed */
  19. #define    CODES    "0123456789n"    /* digit codes                */
  20.  
  21. int    Tics,            /* Soft version of time scale        */
  22.     Time,            /* Current "Time" in tics        */
  23.     Score,            /* Current score            */
  24.     Best,            /* Best score                */
  25.     NCodes,            /* length of CODES            */
  26.     Losses,
  27.     Speed,
  28.     ISpeed,            /* Starting speed            */
  29.     Size,
  30.     KeyDelt,        /* Time per key, computer typein.    */
  31.     KeyTime,
  32.     xx;
  33.  
  34. char    Display[80],        /* the current shooting gallery        */
  35.     *Who,            /* Current player.            */
  36.     WhoArray[100],
  37.     Update,            /* flag: 1 => update file.        */
  38.     IPlay,            /* flag: Computer playing.        */
  39.     NoRec,            /* Don't record this game.        */
  40.     Select;            /* The selected code.            */
  41.  
  42.  
  43. /* Storage of best scores:                        */
  44.  
  45. struct ScRec {            /* Each record 64 bytes long.    */
  46.     int Sc;
  47.     char Name[62];
  48.     } *BRec[10], BStore[10];
  49.  
  50. #define    RECSIZ    64
  51. #define    SCFILE    "CASIO.REC"    /* Name of score file.            */
  52.  
  53. /* Show the top 10 games:                    */
  54.  
  55. Show()
  56.  {    int i, j;
  57.     struct ScRec *p;
  58.     printf("\033H\033JCasio Watch Game\r\n\n\tTOP PLAYERS:\r\n\n");
  59.     for (i=0, j=1; i<10; i++)
  60.      { p = BRec[i];
  61.        if ((p->Name[0]) && (p->Sc))
  62.         printf("%2d.\t%d\t%s\r\n", j++, p->Sc, p->Name);
  63.      }
  64.     printf("\r\n");
  65.  }
  66.  
  67.  
  68. /* Read the score file:
  69.  */
  70.  
  71. RdRec()
  72.  {    int i, j, k;
  73.     struct ScRec *p;
  74.     for (j=0; j<10; j++)
  75.      { p = BRec[j] = &(BStore[j]);
  76.        p->Sc = 0;
  77.        p->Name[0] = 0; }
  78.     i = open(SCFILE, 0);
  79.     if (i >= 0)
  80.         { read(i, BStore, 5);        /* 2 structs/128b record */
  81.           close(i); }
  82.     Update = 0;
  83.  }
  84.  
  85. /* Write the score file:                        */
  86.  
  87. WrRec()
  88.  {    int fid, i, j;
  89.     char buf[640], *cc, *dd;
  90.     if (!Update) return;
  91.     if ((fid = creat(SCFILE)) < 0) return;
  92.     printf("Updating the record ... ");
  93.     for (cc = buf, i=0; i<10; i++)
  94.      { dd = BRec[i];
  95.        for (j=RECSIZ; j--;) *cc++ = *dd++; }
  96.     write(fid, buf, 5);
  97.     close(fid);
  98.     Update = 0;        /* file is now up to date.        */
  99.     printf("\r\n");
  100.  }
  101.  
  102.  
  103. /* Update the score file with the current score:            */
  104.  
  105. Record()
  106.  {    char buf[100], *cc, *dd;
  107.     int i, j;
  108.     struct ScRec *p, *q;
  109.  
  110.     for (i=0; i<10; i++)
  111.      { p = BRec[i];
  112.        if (Score > (p->Sc))
  113.         { q = BRec[9];
  114.           for (j=9; j>i; j--) BRec[j] = BRec[j-1];
  115.           BRec[i] = q;
  116.  
  117.           if (IPlay)
  118.             { printf("Wow, I made the number %d score!\r\n",
  119.                 i+1);
  120.               return; }
  121.  
  122.           printf("Congratulations, you have made the number %d score!",
  123.                 i+1);
  124.  
  125.           if (NoRec) return;
  126.  
  127. ren:          printf("\r\nWho are you, for the record? ");
  128.           for (cc=buf;;)
  129.             { if ((i=getchar()) == 0177) goto ren;
  130.               if ((i == '4') || (i == '6'))
  131.                 if (cc == buf) continue;
  132.               if ((i == '\r') || (i == '\n')) break;
  133.               if (cc == &(buf[61])) ;
  134.               else { putchar(i); *cc++ = i; }}
  135.           *cc = 0;
  136.           if (buf[0])
  137.            {       for (cc=WhoArray, dd=buf; *cc++ = *dd++;);
  138.             Who = WhoArray;
  139.             cc = buf; }
  140.           else cc = Who;
  141.  
  142. mine:          for (dd = (q->Name); *dd++ = *cc++;);
  143.           q->Sc = Score;
  144.           Update = 1;
  145.           printf("\r\n");
  146.           return;
  147.      }}
  148.  }
  149.  
  150. int kbhit()
  151.  {    return inp(CSTAT)&CIMASK; }
  152.  
  153. char getchar()
  154.  {    while (!kbhit());
  155.     return 0177 & inp(CDATA); }
  156.  
  157. putchar(c)
  158.  char c;
  159.  {    while (!(inp(CSTAT) & COMASK));
  160.     outp(CDATA, c); }
  161.  
  162. Delay(when)            /* Wait for ...                */
  163.  {    int i;
  164.     do    { Time++;
  165.           for (i = Tics; i--;)
  166.             if (kbhit() || (IPlay && (Time >= KeyTime))) return; }
  167.     while (Time < when);
  168.  }
  169.  
  170. RanDig()
  171.  {    int i;
  172.     i = nrand(1)>>5;
  173.     i &= 0177;
  174.     i %= NCodes;
  175.     i++;
  176.     return i;
  177.  }
  178.  
  179. Round(speed, Left)
  180.  {    int i, Alarm;
  181.     char ca;
  182.     Speed = speed;
  183.     for (i=0; i<Size; i++) Display[i] = 0;
  184.     Time = 0;
  185.     if (IPlay) KeyTime = KeyDelt;
  186.     Alarm = speed;
  187.     printf("\r\n\t\t\tPartial score: %d   Digits left: %d   ",
  188.         Score, Left);
  189.     Dpy();
  190.  
  191.     do    {
  192.           Delay(Alarm);
  193.  
  194.           if (IPlay)
  195.             {
  196.               if (kbhit()) switch(getchar()) {
  197.                 case 'q':    case 'Q':
  198.                 case 'C'-64:    Losses = 0;
  199.                         return Left;
  200.                 default:    putchar(7);
  201.                 };
  202.  
  203.               if (Time >= KeyTime)
  204.                 { for (i=0; i<Size; i++)
  205.                     if (Select == Display[i])
  206.                         { Shoot(); goto shot; }
  207.                   if (++Select > NCodes) Select=1;
  208.             shot:      Dpy();
  209.                   KeyTime += KeyDelt;
  210.                 }
  211.             }
  212.  
  213.           if (Time >= Alarm)
  214.             { Alarm += speed;
  215.               if (Display[Size-1])
  216.                 { for (i=0; i<Size; i++)
  217.                     if (Display[i]) Left++;
  218.                   return Left;    /* full -- he loses. */
  219.                 }
  220.               ca = 0;
  221.               for (i=(Size-1); i; i--)
  222.                 ca |= (Display[i] = Display[i-1]);
  223.               if (--Left >= 0) Display[0] = RanDig();
  224.               else
  225.                {    Display[0] = 0;    /* Space at end. */
  226.                 Left++;
  227.                 if (!ca) { Dpy(); return 0; }
  228.                }
  229.               Dpy(); }
  230.  
  231.           else if (kbhit() && !IPlay) switch (0177 & getchar()) {
  232.             case 'C':    case 'c':
  233.                     KeyTime = Time+KeyDelt;
  234.                     IPlay = 1;
  235.                     continue;
  236.             case 'q':    case 'Q':
  237.             case 'C'-64:    Losses=0; return Left;
  238.             case ',':
  239.             case '4':    if (++Select > NCodes) Select=1;
  240.                     puts("\33j\33Y. \33K\33k");
  241.                     Dpy(); continue;
  242.             case '.':
  243.             case '6':    Shoot(); continue;
  244.             };
  245.         } while (1);
  246.  }
  247.  
  248. Game()
  249.  {    int speed, i, Left;
  250.     char *cc;
  251.  
  252.     speed = ISpeed;
  253.     
  254.     Score = 0;
  255.     Select = 1;
  256.     Losses = 3;
  257.  
  258.     Show();
  259.  
  260.     for (i=0; 1; i++)
  261.      { Left = 16;
  262.        printf("\r\nRound %d: score is %d.  Speed=%d   ",
  263.         i+1, Score, speed);
  264.        while (Left = Round(speed, Left))
  265.         { if (--Losses <= 0) goto over;
  266.         }
  267.        speed -= (speed >> 3); }
  268.  
  269. over:    printf("\r\nGame over.  Your score is %d\r\n", Score);
  270.  
  271.     Record();
  272.  }
  273.  
  274. Shoot()
  275.  {    int i, s;
  276.     s = 10;
  277.     for (i=(Size-1); i >= 0; i--, s += 10)
  278.         if (Display[i] == Select) {        /* A hit.    */
  279.               puts("\33j\33Y. \33K\33k");
  280.               while (i<(Size-1))
  281.                  {
  282.                    Display[i] = Display[i+1];
  283.                    i++;
  284.                  }
  285.               
  286.               Display[Size-1] = 0;
  287.               Score += s;
  288.               Dpy();
  289.               return; }
  290.     puts("\33j\33Y. Missed!\33k");         /* Missed!    */
  291.     if ((Score -= 30) < 0) Score = 0;
  292.  }
  293.  
  294. Ask(msg)
  295.  {    int i;
  296. agn:    printf("\r\n%s ", msg);
  297.     while (!kbhit()) nrand(1);
  298.     switch(i = getchar())
  299.      { case 'y':    printf("yes\r\n"); return 1;
  300.        case 'n':    printf("no\r\n"); return 0;
  301.        default:    printf(" y or n, please!"); goto agn; }
  302.  }
  303.  
  304. Dpy()
  305.  {    int i, j;
  306.     char *cc;
  307.     cc = CODES;
  308.     cc--;
  309.     printf("\r  ");
  310.     for (i=0; i<Size; i++)
  311.         { if (j = Display[i]) putchar(cc[j]);
  312.           else putchar ('-'); }
  313.     printf("   %c  (%d)  ", cc[Select], Losses);
  314.  }
  315.  
  316. main(argc, argv)
  317.  char **argv;
  318.  {    int i, j;
  319.     char *cc;
  320.     Tics = TICS;        /* Initializations            */
  321.     for (NCodes=0, cc=CODES; cc[NCodes]; NCodes++);
  322.     ISpeed = 140;        /* Starting speed.            */
  323.     Size = SIZE;
  324.     KeyDelt = 60;
  325.     Who = "The Mysterious CASIO Hustler";
  326.     IPlay = 0;
  327.     NoRec = 0;
  328.  
  329.     RdRec();
  330.  
  331.     for (i=1; i<argc; i++)
  332.      {    switch (*(cc = argv[i]))
  333.             {
  334.               case 'S':    ISpeed = atoi(++cc); continue;
  335.               case 'Z':    Size = atoi(++cc); continue;
  336.               case 'K':    KeyDelt = atoi(++cc); continue;
  337.             }
  338.      }
  339.     do    { Show();
  340.           printf("Instructions:\r\n");
  341.           printf("  4    increment 'gun' to next number\r\n");
  342.           printf("  6    shoot.\r\n");
  343.           printf("  q    quit\r\n");
  344.           printf("  c    Computer play\r\n");
  345.           if (!Ask("Another game? ")) break;
  346.           Game(); }
  347.     while (1);
  348.     WrRec();
  349.     exit();
  350.  }
  351.